import { partsStatusMap, markTypeMap } from '@/config/dict'
import { parseTime } from '@/utils/index'
import { CustomStatus } from '@/components'

// 查询 formitem 配置
export const searchFormItem = [
  {
    type: 'input',
    key: 'keyword',
    label: '类型名称'
  },
  {
    type: 'select',
    key: 'enabled',
    label: '状态',
    options: partsStatusMap
    // options: [{ key: 0, value: '全部' }, ...partsStatusMap]
  }
]

// tableColumn 配置
export const tableColumn = (editFn, handleUpdateStatus, configFn, deleteFn) => [
  {
    key: 'typeName',
    label: '类型名称',
    minWidth: 120
  },
  {
    key: 'enabled',
    label: '状态',
    minWidth: 120,
    render: (h, scope) => <CustomStatus list={partsStatusMap} value={scope.row.enabled} />
  },
  {
    key: 'updateName',
    label: '最后修改人',
    minWidth: 120
  },
  {
    key: 'updateTime',
    label: '最后修改时间',
    minWidth: 200,
    render: (h, scope) => parseTime(scope.row.updateTime)
  },
  {
    type: 'button',
    label: '操作',
    list: [
      {
        name: '修改',
        authKey: 'b_2_2',
        click: editFn
      },
      {
        name: (scope) => !scope.row.enabled ? '启用' : '停用',
        authKey: 'b_2_4',
        click: (id, row) => handleUpdateStatus(id, !row.enabled)
      },
      {
        name: '属性配置',
        authKey: 'b_2_5',
        otherAttrs: { style: { marginLeft: '10px' }},
        click: configFn
      },
      {
        name: '删除',
        authKey: 'b_2_3',
        confirm: '是否删除?',
        click: deleteFn
      }
    ]
  }
]

// 弹窗 fromitem 配置
export const renderFormItem = (handleChagne, isHide, handleBeforeUpload, handleUploadSuccess, handleExceed, rowId = 0, fileList = null) => {
  return [
    {
      type: 'input',
      key: 'typeName',
      label: '类型名称'
    },
    {
      type: 'select',
      key: 'markType',
      label: '位置标注方式',
      slotAttrs: {
        options: markTypeMap
      },
      slotListeners: {
        change: handleChagne
      }
    },
    {
      type: 'upload',
      key: 'icon',
      isHide,
      label: '部件图标',
      slotAttrs: {
        data: {
          tableName: 'Component',
          attachType: 1,
          rowId
        },
        beforeUpload: (file) => handleBeforeUpload(file),
        onSuccess: (res, file) => handleUploadSuccess(res.data.attach_url),
        onExceed: () => handleExceed(),
        fileList: fileList
      }
    },
    {
      type: 'radio',
      key: 'enabled',
      label: '状态',
      slotAttrs: {
        options: partsStatusMap
      }
    }
  ]
}

// rules 规则
export const formRules = {
  typeName: [{ required: true, message: ' 请输入类型名称!', trigger: 'blur' }],
  markType: [{ required: true, message: '请选择位置标注方式!', trigger: 'change' }],
  enabled: [{ required: true, message: '请选择状态!', trigger: 'change' }]
}

export const formButtons = (onSubmit, onReset) => {
  return {
    isDialog: true,
    submitName: '提交',
    resetName: '取消',
    submitCallback: values => onSubmit(values),
    resetCallback: () => onReset()
  }
}
